<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 03-09
 * Time: 11:09
 */

namespace app\common\library;


use think\Exception;
use think\exception\ErrorException;
use think\exception\PDOException;
use think\exception\DbException;
use think\Model;

class Common extends Model
{
    public static $exceptMsg='';
    public static $getLastInsID = false;
    public static $replace = false;
    public static $affectedRows = 0;

    protected static function affectedRows()
    {
        self::$affectedRows = self::selfConnection()->getNumRows();
    }

    public static function selfSequence()
    {
        return self::sequence();
    }
    public static function selfConnection()
    {
        return self::selfSequence()->getConnection();
    }

    public static function getCurrentSql()
    {
        return self::selfConnection()->getLastSql();
    }

    public static function setIncEx($map,$field,$value=1)
    {
        try{
            self::where($map)->setInc($field,$value);
            self::affectedRows();
            return self::$affectedRows;
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }

    public static function setDecEx($map,$field,$value=1)
    {
        try{
            self::where($map)->setDec($field,$value);
            self::affectedRows();
            return self::$affectedRows;
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }

    public static function add($map)
    {
        try{
            $insert_id = self::insert($map,self::$replace,self::$getLastInsID);
            self::affectedRows();
            return $insert_id;
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }

    public static function del($map)
    {
        try{
            self::where($map)->delete();
            self::affectedRows();
            return self::$affectedRows;
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }

    public static function number($map)
    {
        try{
            self::where($map)->count(1);
            self::affectedRows();
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }

    public static function clear()
    {
        try{
            self::execute("TRUNCATE `a_taobao_coupon`");
            self::affectedRows();
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }

    public static function edit($map,array $update)
    {
        try{
            self::where($map)->update($update);
            self::affectedRows();
            return self::$affectedRows;
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }

    public static function search($map=[],$extParam=[])
    {
        try {
            $query = self::where($map);
            if ($extParam) {
                $listRows = 20;
                if(isset($extParam['where']))
                {
                    foreach ($extParam['where'] as $extWhere)
                    {
                        $query->where($extWhere['field'],$extWhere['op'],$extWhere['condition']);
                    }
                }
                if (isset($extParam['field'])) {
                    $fields = explode(",", $extParam['field']);
                    $count = count($fields);
                    if ($count == 1) {
                        $result = $query->value($fields[0]);
                        self::affectedRows();
                        return $result;
                    } else {
                        if (!empty($fields)) {
                            $query->field($extParam['field']);
                        }
                    }
                }
                if(isset($extParam['group_by']))
                {
                    $query->group($extParam['group_by']);
                }
                if(isset($extParam['order_by']))
                {
                    $query->order($extParam['order_by']);
                }
                if (isset($extParam['limit']) && !empty($extParam['limit'])) {
                    if (is_numeric($extParam['limit'])) {
                        if ($extParam['limit'] <= 1) {
                            $result = $query->find();
                            self::affectedRows();
                            return $result;
                        }
                        $offset = 0;
                        $length = $extParam['limit'];
                    } else if (is_array($extParam['limit'])) {
                        $offset = isset($extParam['limit']['offset']) ? $extParam['limit']['offset'] : 0;
                        $length = isset($extParam['limit']['length']) ? $extParam['limit']['length'] : 1;
                    } else if (is_string($extParam['limit'])) {
                        list($tlength, $toffset) = explode(",", str_ireplace('limit', '', $extParam['limit']));
                        $offset = $toffset ? $toffset : 0;
                        $length = $tlength ? $tlength : 1;

                    } else {
                        $offset = 0;
                        $length = 1;
                    }
                    if ($offset == 0 && $length == 1) {
                        $result = $query->find();
                        self::affectedRows();
                        return $result;
                    }
                    $query->limit($offset, $length);
                } else if (isset($extParam['page']) && !empty($extParam['page'])) {
                    if (is_numeric($extParam['page'])) {
                        if ($extParam['page'] <= 1) {
                            $page = 1;
                        } else {
                            $page = $extParam['page'];
                        }

                    } else if (is_array($extParam['page'])) {
                        $page = isset($extParam['page']['num']) ? $extParam['page']['num'] : 1;
                        $listRows = isset($extParam['page']['size']) ? $extParam['page']['size'] : $listRows;
                    } else if (is_string($extParam['page'])) {
                        list($lsize, $lpage) = explode(",", str_ireplace('page', '', $extParam['page']));
                        $page = $lpage ? $lpage : 1;
                        $listRows = $lsize ? $lsize : $listRows;
                    } else {
                        $page = 1;
                    }
                    $query->page($page, $listRows);
                }
            }
            $result = $query->select();
            self::affectedRows();
            return $result;
        }catch (ErrorException $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }catch (PDOException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (DbException $e){
            self::$exceptMsg = $e->getMessage();
            return false;
        }catch (Exception $e){
            self::$exceptMsg = $e->getMessage();
            self::$affectedRows = -1;
            return false;
        }
    }
}